package com.briup.myblog.service.impl;

import com.briup.myblog.bean.basic.Carousel;
import com.briup.myblog.dao.basic.CarouselMapper;
import com.briup.myblog.dao.ext.CarouselMapperExt;
import com.briup.myblog.exception.CustomerException;
import com.briup.myblog.service.ICarouselService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarouselServiceImpl implements ICarouselService {
    @Autowired
    private CarouselMapper mapper;

    @Autowired
    private CarouselMapperExt mapperExt;

    //查找轮播图
    @Override
    public List<Carousel> findAllCarousel() {
        List<Carousel> list = mapperExt.findAllCarousel();
        return list;
    }

    //删除轮播图
    @Override
    public void deleteById(int id){
        Carousel carousel = mapper.selectByPrimaryKey(id);
        int num = carousel.getCarouselStatus();
        if(num == 1) {
            throw new CustomerException("不能删除启用状态的轮播图");
        }else if ( carousel != null){
            mapper.deleteByPrimaryKey(id);
        }else {
            throw new CustomerException("轮播图不存在");
        }
    }

    //分页查询轮播图
    @Override
    public PageInfo<Carousel> findAll(int pageSize, int pageNum) {
        PageHelper.startPage(pageSize, pageNum);
        List<Carousel> list = mapperExt.findAll();
        PageInfo<Carousel> carouselPageInfo = new PageInfo<>(list);
        return carouselPageInfo;
    }

    //保存轮播图
    @Override
    public void saveCarousel(Carousel carousel) {
        //当添加的轮播图的url重复时，提示用户无法添加重复的url
        ///if(){ throw new xxxx}
        //当添加轮播图的名称重复时，提示用户不能添加
        //if(){ throw  xxxxx}
        //对象有3个属性值 通过前端   2个值 status  = 0

        String url = carousel.getCarouselUrl();
        String name = carousel.getCarouselName();
        Carousel carouselUrl = mapperExt.findCarouselUrl(url);
        Carousel carouselName = mapperExt.findCarouselName(name);
        if(carouselUrl != null) {
            throw new CustomerException("轮播图地址已存在");
        }
        if(carouselName != null) {
            throw new CustomerException("轮播图名称已存在");
        }

        carousel.setCarouselStatus(0);//默认添加的轮播图状态为禁用0
        mapper.insert(carousel);

    }

    //修改轮播图
    @Override
    public void updateCarousel(Carousel carousel) {
        //1.如果更新的轮播图的基本信息
        //2.如果更新的轮播图的状态 id  status  其他值为null

        //查找启用状态为1的轮播图
        List<Carousel> list = mapperExt.findAllCarousel();

        String name = carousel.getCarouselName();
        String url = carousel.getCarouselUrl();
        int num = carousel.getCarouselId();

        Carousel carousel1 = mapperExt.findCarouselName(name, num);
        Carousel carousel2 = mapperExt.findCarouselUrl(url, num);

        String name1 = carousel.getCarouselName();
        //判断只有一个启动轮播图的id 与 要禁用的id是否相等
        if (list.size() == 1){
            int i = list.get(0).getCarouselId();
            if(i == num){
                //判断当前修改的内容是否有值
                if (name1 != null){
                    mapper.updateByPrimaryKeySelective(carousel);
                }else {
                    throw new CustomerException("无法全部禁用轮播图");
                }
            }
        }

        //判断轮播图名称是否存在
        if (carousel1 != null) {
            throw new CustomerException("轮播图名称已存在");
        }else {
            mapper.updateByPrimaryKeySelective(carousel);
        }

        //判断轮播图路径是否存在
        if (carousel2 != null){
            throw new CustomerException("轮播图路径已存在");
        }else {
            mapper.updateByPrimaryKeySelective(carousel);
        }

    }

}
